package com.hzf.booklibrary.ui.presenter;

import android.content.Context;
import android.util.Log;

import com.hzf.booklibrary.ReaderApplication;
import com.hzf.booklibrary.api.BookApi;
import com.hzf.booklibrary.base.RxPresenter;
import com.hzf.booklibrary.bean.BookClassify;
import com.hzf.booklibrary.ui.contract.BookClassifyContract;

import java.util.List;

import javax.inject.Inject;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/4/12.
 */

public class BookClassifyPresenter extends RxPresenter<BookClassifyContract.View> implements BookClassifyContract.Presenter<BookClassifyContract.View> {
    private Context mContext;
    private BookApi bookApi;

    private static final String TAG = "BookClassifyPresenter";
    @Inject
    public BookClassifyPresenter(Context mContext, BookApi bookApi){
        this.mContext =mContext;
        this.bookApi=bookApi;
    }
    @Override
    public void getBookClassify() {
        Subscription rxSubscription = bookApi.getBookClassify().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BookClassify>() {
                    @Override
                    public void onNext(BookClassify data) {
                        List<BookClassify.Classify> list = data.result;
                        if (list != null && mView != null&&!list.isEmpty()) {
                            mView.showBookClassify(list);
                        }
                    }
                    @Override
                    public void onCompleted() {
                        mView.complete();
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: " + e);
                        if(mView!=null){
                            mView.showError();
                        }
                        ReaderApplication.getsInstance().initIpAdress();//重新获取IP地址
                    }
                });
        addSubscrebe(rxSubscription);
    }
}
